﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;

namespace VIT.Web.Admin
{
    public partial class LinkEdit : VITComponent
    {
        Provider.Link obj;

        protected void Page_Load(object sender, EventArgs e)
        {            
            obj = new Provider.Link();
            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadData();
                loadLanguages();                
            }
        }

        #region Methods
        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();

            if (obj.Id >0 )
            {
                txtTitle.Text = obj.Name;
                txtURL.Text = obj.URL;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
            }
        }

        private void loadLanguages()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void save()
        {                
                obj.LanguageId = ddlLang.SelectedValue;
                obj.Name = txtTitle.Text;
                obj.URL = txtURL.Text;
                obj.Orders = int.Parse(txtOrder.Text);
                obj.Published = chkPublish.Checked;

                if (obj.Save() > 0) setTrueVisible(lblThanhCong);
                else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Link", "LinkList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Link", "LinkList");
        }
        #endregion
    }
}
